package name.matthewgreet.example2.action; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.stream.Collectors; import javax.servlet.http.HttpSession; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Actions; import org.apache.struts2.convention.annotation.InterceptorRef; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import com.opensymphony.xwork2.ActionSupport; import name.matthewgreet.example2.action.FindGameListByDesignerNameAction.GameIdFinderByDesigner; import name.matthewgreet.example2.action.FindGameListByFamilyAction.GameListFinderByFamily; import name.matthewgreet.example2.action.FindGameListByNameContainsAction.GameListFinderByNameContains; import name.matthewgreet.example2.action.FindGameListByRankAction.GameListSizeFinderByRank; import name.matthewgreet.example2.action.FindGameListByRankAction.GamePageFinderByRank; import name.matthewgreet.example2.common.AbstractViewActionSupport; import name.matthewgreet.example2.common.ListCache; import name.matthewgreet.example2.common.ListFinder; import name.matthewgreet.example2.common.PageByIdsFinder; import name.matthewgreet.example2.common.PageExtensionAssembler; import name.matthewgreet.example2.dto.GameDTO; import name.matthewgreet.example2.exception.ConfigurationException; import name.matthewgreet.example2.common.ListCache.ListCacheConfig; import name.matthewgreet.example2.common.ListCache.PaginationMode; import name.matthewgreet.example2.form.DesignerNameForm; import name.matthewgreet.example2.form.GameFamilyForm; import name.matthewgreet.example2.form.GameNameContainsForm; import name.matthewgreet.example2.form.GameRankForm; import name.matthewgreet.example2.model.DesignerTable; import name.matthewgreet.example2.model.GameTable; import name.matthewgreet.example2.model.Model; import name.matthewgreet.example2.model.GameTable.GameOption; import name.matthewgreet.example2.view.GameListDisplay; /** * Displays search results. */ @InterceptorRef("ViewStack") @Results({ @Result(name=ActionSupport.SUCCESS, location="/pages/GameList.jsp") }) public class ViewGameListAction extends AbstractViewActionSupport { public static class GameListFinderEmpty implements ListFinder<GameDTO> { private static final long serialVersionUID = -6857353849968559475L; @Override public List<GameDTO> getList() throws Exception { return new ArrayList<>(); } } public static class GamePageByIdsFinder implements PageByIdsFinder<Integer,GameDTO> { private static final long serialVersionUID = 4032331794734656685L; @Override public Collection<GameDTO> getItems(Collection<Integer> keys) throws Exception { GameTable gameTable; Collection<GameDTO> result; gameTable = Model.getInstance().getGameTable(); result = gameTable.findByIds(keys, GameOption.DESIGNERS); return result; } } public static class GamePageExtensionAssembler implements PageExtensionAssembler<GameDTO> { private static final long serialVersionUID = 2206130689287406339L; @Override public void assemblePageExtensions(Collection<GameDTO> items) throws Exception { DesignerTable designerTable; designerTable = Model.getInstance().getDesignerTable(); for (GameDTO game: items) { game.setDesigners(designerTable.findByGameId(game.getId())); } } } public enum GameCategory {ALL, STRATEGY, ABSTRACT, FAMILY, THEMATIC, CUSTOMIZABLE, WAR, PARTY, CHILDREN} private static final long serialVersionUID = -7979376337555804889L; private static final String ATTR_NAME_LIST_CACHE = ViewGameListAction.class + "_LIST_CACHE"; private static final int PAGE_SIZE = 7; @SuppressWarnings("unchecked") public static ListCache<Integer,GameDTO> retrieveListCache(HttpSession session) { ListCache<Integer,GameDTO> result; ListCacheConfig<Integer,GameDTO> config; result = (ListCache<Integer,GameDTO>)session.getAttribute(ATTR_NAME_LIST_CACHE); if (result == null) { config = new ListCacheConfig<Integer,GameDTO>(); config.setKeyExtractor(g -> g.getId()); config.setListSizeFinder(new GameListSizeFinderByRank(GameCategory.ALL, null, null)); //config.setListFinder(new GameListFinderEmpty()); config.setName("board games"); config.setPageByIdsFinder(new GamePageByIdsFinder()); config.setPageByIndexRangeFinder(new GamePageFinderByRank(GameCategory.ALL, null, null)); config.setPageExtensionAssembler(new GamePageExtensionAssembler()); config.setPageSize(PAGE_SIZE); config.setPaginationMode(PaginationMode.PAGE_BY_INDEX_RANGE); //config.setPaginationMode(PaginationMode.FULL_LIST); result = new ListCache<Integer,GameDTO>(config); session.setAttribute(ATTR_NAME_LIST_CACHE, result); } return result; } private String refresh; private Integer pageNo; private GameNameContainsForm gameNameContainsForm; private GameRankForm gameRankForm; private DesignerNameForm designerNameForm; private GameFamilyForm gameFamilyForm; private GameListDisplay listDisplay; private String designersAutocompleteText; private String familiesAutocompleteText; private String makeDesignersAutocompleteText() throws ConfigurationException { DesignerTable designerTable; String result; // This is an obvious candidate for caching but that's not being demonstrated designerTable = Model.getInstance().getDesignerTable(); result = designerTable.findAll().stream().map(d -> d.getName().replace("\"", "\\\"")).sorted().collect(Collectors.joining("\",\n\"","\"", "\"")); return result; } private String makeFamiliesAutocompleteText() throws ConfigurationException { GameTable gameTable; String result; // This is an obvious candidate for caching but that's not being demonstrated gameTable = Model.getInstance().getGameTable(); result = gameTable.getFamilies().stream().map(d -> d.replace("\"", "\\\"")).sorted().collect(Collectors.joining("\",\n\"","\"", "\"")); return result; } @Actions({ @Action("/ViewGameList"), @Action("/") }) @Override public String execute() throws Exception { GameIdFinderByDesigner gameIdFinderByDesigner; GameListFinderByFamily gameListFinderByFamily; GameListFinderByNameContains gameListFinderByNameContains; GameListSizeFinderByRank gameListSizeFinderByRank; ListCache<Integer,GameDTO> listCache; HttpSession session; GameCategory preferredCategory; session = getRequest().getSession(); listCache = retrieveListCache(session); if (refresh != null) { listCache.forceReload(); } if (pageNo != null) { listCache.setPageNo(pageNo); } preferredCategory = GameCategory.ALL; if (gameNameContainsForm == null) { if (listCache.getBaseRecordListFinder() instanceof GameListFinderByNameContains) { gameListFinderByNameContains = (GameListFinderByNameContains)listCache.getBaseRecordListFinder(); gameNameContainsForm = new GameNameContainsForm(gameListFinderByNameContains.getNameContains()); } else { gameNameContainsForm = new GameNameContainsForm(); } } if (gameRankForm == null) { if (listCache.getListSizeFinder() instanceof GameListSizeFinderByRank) { gameListSizeFinderByRank = (GameListSizeFinderByRank)listCache.getListSizeFinder(); preferredCategory = gameListSizeFinderByRank.getCategory(); gameRankForm = new GameRankForm(gameListSizeFinderByRank.getCategory(), gameListSizeFinderByRank.getRankRangeStart(), gameListSizeFinderByRank.getRankRangeEnd()); } else { gameRankForm = new GameRankForm(); } } if (designerNameForm == null) { if (listCache.getIdListFinder() instanceof GameIdFinderByDesigner) { gameIdFinderByDesigner = (GameIdFinderByDesigner)listCache.getIdListFinder(); designerNameForm = new DesignerNameForm(gameIdFinderByDesigner.getDesignerName()); } else { designerNameForm = new DesignerNameForm(); } } if (gameFamilyForm == null) { if (listCache.getListFinder() instanceof GameListFinderByFamily) { gameListFinderByFamily = (GameListFinderByFamily)listCache.getListFinder(); gameFamilyForm = new GameFamilyForm(gameListFinderByFamily.getFamily()); } else { gameFamilyForm = new GameFamilyForm(); } } listDisplay = new GameListDisplay(listCache, preferredCategory); listDisplay.init(); designersAutocompleteText = makeDesignersAutocompleteText(); familiesAutocompleteText = makeFamiliesAutocompleteText(); return SUCCESS; } public String getRefresh() { return refresh; } public void setRefresh(String refresh) { this.refresh = refresh; } public Integer getPageNo() { return pageNo; } public void setPageNo(Integer pageNo) { this.pageNo = pageNo; } public GameNameContainsForm getGameNameContainsForm() { return gameNameContainsForm; } public GameRankForm getGameRankForm() { return gameRankForm; } public DesignerNameForm getDesignerNameForm() { return designerNameForm; } public GameFamilyForm getGameFamilyForm() { return gameFamilyForm; } public GameListDisplay getListDisplay() { return listDisplay; } public String getDesignersAutocompleteText() { return designersAutocompleteText; } public String getFamiliesAutocompleteText() { return familiesAutocompleteText; } }